package org.swimv2.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.swimv2.controller.GestioneAmicizia;
import org.swimv2.controller.GestioneAmiciziaRemote;
import org.swimv2.controller.GestioneRicerca;
import org.swimv2.controller.GestioneRicercaRemote;
import org.swimv2.model.Persona;

/**
 * Servlet implementation class SuggerimentiServlet
 */
public class SuggerimentiServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SuggerimentiServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
		props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		Context ctx,ctx1;
		Persona utente=(Persona) request.getSession().getAttribute("utente");
		
		try{
			// MI SERVE PER CERCARE GLI AMICI DEL NUOVO UTENTE
			ctx = new InitialContext(props);
			Object obj = ctx.lookup(GestioneRicerca.JNDI_NAME);
			GestioneRicercaRemote manager = (GestioneRicercaRemote)PortableRemoteObject.narrow(obj,GestioneRicercaRemote.class);
			// MI SERVE PER IL CHECK AMICIZIE
			ctx1 = new InitialContext(props);
			Object obj1 = ctx1.lookup(GestioneAmicizia.JNDI_NAME);
			GestioneAmiciziaRemote manager1 = (GestioneAmiciziaRemote)PortableRemoteObject.narrow(obj1,GestioneAmiciziaRemote.class);
			
			String userId = utente.getUserID();
			Collection <Persona> temp123 = manager.getAmiciUtente(utente.getUserID());
			Collection <Persona> amici = new ArrayList<Persona>();
			for(Persona p : temp123)
			{
				if(!manager1.checkIndiretta(userId, p.getUserID()))
					{
					amici.add(p);
					}
			}
			
			List<Persona> amiciDiAmico=new ArrayList<Persona>();
			
			for(Persona p: amici){
				List<Persona> temp2 = (List<Persona>)manager.getAmiciUtente(p.getUserID());
				for(Persona s:temp2)
				{
					if(!s.equals(utente)  && !amiciDiAmico.contains(s) && !manager1.checkAmicizia(s.getUserID(),userId)){
						amiciDiAmico.add(s);
					}
				}
			}
			
			
			
			request.setAttribute("lstSuggerimenti", amiciDiAmico);
			request.getRequestDispatcher("WEB-INF/suggerimenti.jsp").
			forward(request, response);			

		}
		catch(NamingException e){
			e.printStackTrace();
			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
